package com.mifan.mapper;

import com.mifan.entity.Resources;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mifan.vo.ResourceListVo;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 资源表 Mapper 接口
 * </p>
 *
 * @author Milan
 * @since 2024-09-13
 */
public interface ResourceMapper extends BaseMapper<Resources> {

    /**
     * 根据用户 username 获取资源
     *
     * @param username
     * @return
     */
    @Select("SELECT r.code\n" +
            "FROM ums_resource r\n" +
            "         LEFT JOIN ums_role_resource rr ON r.id = rr.resource_id\n" +
            "         LEFT JOIN ums_role ur ON ur.id = rr.role_id\n" +
            "         LEFT JOIN usm_user_role ar ON ur.id = ar.role_id\n" +
            "         LEFT JOIN ums_user a ON a.id = ar.user_id\n" +
            "WHERE a.username = #{username};")
    List<Resources> getResourcesByUserUsername(String username);



    /**
     * 获取所有资源及其菜单名称
     * @return
     */
    @Select("select ur.*,um.title as menu_name from ums_resource ur left join ums_menu um on ur.menu_id = um.id")
    @Results(
            {
                @Result(column = "id", property = "id"),
                @Result(column = "code", property = "code"),
                @Result(column = "name", property = "name"),
                @Result(column = "url", property = "url"),
                @Result(column = "menu_id", property = "menuId"),
                @Result(column = "description", property = "description"),
                @Result(column = "menu_name", property = "menu")
            }
    )
    List<ResourceListVo> selectListWithMenu();
}
